package com.rp.system.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rp.common.aspect.annotetion.AutoLog;
import com.rp.common.aspect.annotetion.ResponseResultBody;
import com.rp.common.result.ResultDTO;
import com.rp.system.user.entity.SysRole;
import com.rp.system.user.service.SysRoleService;
import com.rp.common.utils.Selectway;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/sys-role")
@Api(tags = "角色管理")
@ResponseResultBody
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private Selectway selectway;


    @GetMapping("/list")
    @ApiOperation(value = "分页列表")
    @AutoLog(value = "角色管理-分页列表")
    @RequiresPermissions("sysRole:list")
    public ResultDTO<IPage<SysRole>> list(
            @RequestParam(defaultValue = "1",required = false) @ApiParam(value = "当前页面") int pageNum,
            @RequestParam(defaultValue = "20",required = false) @ApiParam(value = "每页显示的数量") int pageSize,
            @RequestParam(required = false) @ApiParam(value = "模糊查询字符串") String string){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
        selectway.fields(queryWrapper,string,"sys_role",null,null);
        queryWrapper.orderByDesc("id");

        return ResultDTO.success().data(sysRoleService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @RequiresPermissions("sysRole:update")
    @PostMapping("/insert")
    @AutoLog(value = "角色管理-增加")
    @ApiOperation(value = "增加")
    public ResultDTO<IPage<SysRole>> insert(@RequestBody SysRole sysRole){

        return ResultDTO.tf(sysRoleService.save(sysRole));
    }

    @RequiresPermissions("sysRole:update")
    @PutMapping("/update")
    @AutoLog(value = "角色管理-修改")
    @ApiOperation(value = "修改")
    public ResultDTO<IPage<SysRole>> update(@RequestBody SysRole sysRole){
        return ResultDTO.tf(sysRoleService.updateById(sysRole));
    }

    @RequiresPermissions("sysRole:update")
    @DeleteMapping("/delete")
    @AutoLog(value = "角色管理-删除")
    @ApiOperation(value = "删除")
    public ResultDTO delete(@RequestBody @ApiParam(value = "id", required = true) String id){
        return ResultDTO.tf(sysRoleService.removeById(id));
    }

    @RequiresPermissions("sysRole:update")
    @DeleteMapping("/deleteAll")
    @AutoLog(value = "角色管理-批量删除")
    @ApiOperation(value = "批量删除")
    public ResultDTO deleteAll(@RequestBody @ApiParam(value = "id(英文逗号隔开)", required = true) String ids){
        return ResultDTO.tf(sysRoleService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @GetMapping("/listAll")
    @ApiOperation(value = "不分页列表")
    @AutoLog(value = "角色管理-不分页列表")
    @RequiresPermissions("sysRole:list")
    public ResultDTO<IPage<SysRole>> listAll(){
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
        queryWrapper.orderByDesc("create_time");
        return ResultDTO.success().data(sysRoleService.list(queryWrapper));
    }
}
